package com.jf.cloud.service.sysmanager.remote;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jf.cloud.service.sysmanager.common.base.AjaxPageData;
import com.jf.cloud.service.sysmanager.common.base.OPaging;
import com.jf.cloud.service.sysmanager.common.db.Query;
import com.jf.cloud.service.sysmanager.common.util.DateUtil;
import com.jf.cloud.service.sysmanager.dao.ISysAppVersionService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleService;
import com.jf.cloud.service.sysmanager.dao.ISysRoleTypeService;
import com.jf.cloud.service.sysmanager.dao.ISysStaffService;
import com.jf.cloud.service.sysmanager.entity.SysAppVersion;
import com.jf.cloud.service.sysmanager.entity.SysStaff;
import com.jf.cloud.service.sysmanager.support.web.BaseController;

/**
 * APP 版本控制
 * @author roway
 * @since 2017年12月12日下午2:51:07
 */
@Controller
@RequestMapping("/sysAppVersion")
public class SysAppVersionController extends BaseController {
	
	private static Log log = LogFactory.getLog(SysAppVersionController.class);
	
	@Resource
	private ISysAppVersionService appVersionService;
	
	@Resource
	private ISysStaffService staffService;
	
	@Resource
	private ISysRoleService roleService;

	@Resource
	private ISysRoleTypeService roleTypeService;
	
	/**
	 * 列表页面跳转
	 * @author roway
	 * @since 2017年12月12日下午3:30:16
	 */
	@RequestMapping("/list")
	public String list(HttpServletRequest request){
		Integer flag = this.getIntValue(request, "flag");
		if (Integer.valueOf(1).equals(flag)) {
			request.setAttribute("msg", "添加成功");
		}
		try {
			request.setAttribute("roleNameList", getJSONDict(roleService.findForTags("ROLE_NAME", "ROW_ID", new Query())));
		} catch (IOException e) {
			log.error("到版本管理页面出错了", e);
			request.setAttribute("msg", "到版本管理页面出错了!");
		}
		return "jsp/system/appVersion_list";
	}
	
	/**
	 * 列表页面数据获取
	 * @author roway
	 * @since 2017年12月12日下午3:31:34
	 */
	@ResponseBody
	@RequestMapping("/appVersion.json")
	public  AjaxPageData<SysAppVersion> list_json (HttpServletRequest request, OPaging<SysAppVersion> page) {  
		//page.getWm().put("DEL_FLAG", "0");//只获取未删除的员工

		return getAjaxPageData(appVersionService.findPage(page));
	}
	
	/**
	 * 去编辑 版本控制页面
	 * @author roway
	 * @since 2017年12月13日下午2:11:07
	 */
	@RequestMapping("/editor")
	public String editor(HttpServletRequest request,HttpServletResponse response,Long uid) {
		try {
			if(uid!=null && uid>0){ //修改  使用
				SysAppVersion appVersion = appVersionService.findAppVersion(uid);
				request.setAttribute("appVersion", appVersion);
			}
		} catch (Exception e) {
			log.error("去 编辑 版本控制 页面出错", e);
		}
		return "jsp/system/appVersion_editor";
	}
	
	/**
	 * 去添加 版本控制页面
	 * @author roway
	 * @since 2017年12月13日下午2:12:19
	 */
	@RequestMapping("/toAddAppVersion")
	public String toAddAppVersion(HttpServletRequest request,HttpServletResponse response) {
		return "jsp/system/appVersion_add";
	}
	
	/**
	 * 新增 版本控制
	 * @author roway
	 * @since 2017年12月13日下午2:12:48
	 */
	@RequestMapping("/save")
	public String saveStaff(HttpServletRequest request,SysAppVersion appVersion) {
		String returnView = list(request);
		if (isReCommit(request)) {
			return "redirect:/sysAppVersion/list?flag=1";
		}
		try {
			switch (appVersion.getPlatformType()) {
			case 1:
				appVersion.setPlatformTypeName("IPhone");
				break;
			case 3:
				appVersion.setPlatformTypeName("Android Phone");
				break;
			default:
				break;
			}
			appVersion.setPubTime(DateUtil.getCurrentDateTime());
			appVersion = appVersionService.saveAppVersion(appVersion);
			
			request.setAttribute("msg", " 添加版本控制成功。");
		} catch (Exception e) {
			log.error("保存版本控制出错了", e); 
			request.setAttribute("msg", " 添加版本控制失败。 囧   ");
		}
		return returnView;
	}
	
	/**
	 * 修改 版本控制
	 * @author roway
	 * @since 2017年12月13日下午2:13:11
	 */
	@RequestMapping("/update")
	public String update(HttpServletRequest request,SysAppVersion appVersion) {
		String returnView = list(request);
		try {
			//SysStaff employee = (SysStaff) request.getSession().getAttribute(SysConstant.USER_SESSION_KEY);
			SysStaff employee = getLoginUser(request);
			SysAppVersion updateAppVersion = this.appVersionService.saveOrUpdateAppVersion(appVersion);
			this.appVersionService.saveAppVerSionHistoey(employee,updateAppVersion.getRowId());
			request.setAttribute("msg", " [修改] 版本控制成功。");
		} catch (Exception e) {
			log.error("修改 版本控制出错了", e); 
			request.setAttribute("msg", " [修改] 版本控制失败。 囧   ");
		}
		return returnView;
	}

}
